package com.app.controller.backstage;

import com.app.mapper.NewsMapper;
import com.app.model.AdminUserModel;
import com.app.model.NewsModel;
import com.app.model.VideoTrainModel;
import com.app.service.AdminUserService;
import com.app.service.VideoTrainService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;


@Controller
@RestController
public class VideoTrainBkController {

    @Autowired
    AdminUserService adminUserService;
    @Autowired
    VideoTrainService videoTrainService;


    @GetMapping("bakGetVideoList")// 后台查视频列表
    public Result<Object> bakGetVideoList(HttpServletRequest request,
                              @RequestParam(required = false) String keyWord,
                              @RequestParam(required = false) String startTime,
                              @RequestParam(required = false) String endTime,
                              int page,
                              int limit){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        VideoTrainModel model = new VideoTrainModel();
        model.setPage(page);
        model.setLimit(limit);
        model.setKeyWord(keyWord);
        model.setStartTime(startTime);
        model.setEndTime(endTime);
        return Result.success(videoTrainService.videoList(model));
    }

    @GetMapping("deleteVideoById")//假删除
    public Result<Object> deleteById(HttpServletRequest request,
                             @RequestParam("id") int id,
                             @RequestParam("state") int state
    ){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        VideoTrainModel model = videoTrainService.getVideoById(id);
        if(model==null){
           Result.error(500,"空值或者传参错误");
        }
        model.setDeleteState(state);
        videoTrainService.update(model);

        return Result.success("操作成功");
    }

    //根据id查询视频列表
    @GetMapping("getVideoTrainById")
    public Result<Object> getVideoTrainById( int id,
                                     HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        VideoTrainModel model = videoTrainService.getVideoById(id);
        return Result.success(model);
    }

    @PostMapping("insertAndUpdateVideoTrain")
    public String insertNews(HttpServletRequest request,
                             @RequestBody VideoTrainModel model){
        JSONObject json = new JSONObject();
        if(model.getId()==0){
            model.setCrtTime(String.valueOf(CommonHelp.getNowTimestamp()));
            videoTrainService.insert(model);
        }else {
            videoTrainService.update(model);
        }
        json.put("code", 200);
        json.put("msg", "成功");
        return json.toString();
    }
}
